package org.thread.test05.demo02;

import java.util.concurrent.TimeUnit;

/**
 * @author ZJL
 * @date 2025-03-11
 */
public class TicketThread2 implements Runnable {
    private int ticket = 500;
    private String lock = "";

    public TicketThread2() {

    }

    @Override
    public void run() {
        while (true) {
                if (cell()) break;
        }

    }

    private synchronized boolean cell() {
        if (ticket <= 0) {
            System.out.println("票卖完了！");
            return true;
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        ticket--;
        System.out.println(Thread.currentThread().getName() + "卖出了一张票，还剩" + (ticket) + "票！");
        return false;
    }
}
